<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>千泓网络科技后台管理</title>
        <meta name="renderer" content="webkit">
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
        <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
        <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" />
        <link rel="stylesheet" href="__STATIC__/admin/css/font.css">
        <link rel="stylesheet" href="__STATIC__/admin/css/xadmin.css">
        <link rel="stylesheet" href="__STATIC__/admin/css/main.css">
        <link rel="stylesheet" href="__STATIC__/admin/lib/layui/css/layui.css">

        <link href="http://netdna.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.css" rel="stylesheet">
        <!-- <script src="http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.js"></script>  -->
        
        <!-- //这个css和js请到官网进行下载（点击上面的summernote就可以直达官网） -->
        <link href="http://cdnjs.cloudflare.com/ajax/libs/summernote/0.8.2/summernote.css" rel="stylesheet">
        <script src="__STATIC__/admin/js/jquery.js"></script>
         <link href="http://netdna.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.css" rel="stylesheet">
        <!-- <script src="http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.js"></script>  -->
        <script src="http://netdna.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.js"></script>
        <!-- //这个css和js请到官网进行下载（点击上面的summernote就可以直达官网） -->
        <link href="http://cdnjs.cloudflare.com/ajax/libs/summernote/0.8.2/summernote.css" rel="stylesheet">
        <script src="__STATIC__/admin/js/summernote.js"></script>
        <script src="https://cdn.bootcss.com/summernote/0.8.8/lang/summernote-zh-CN.min.js"></script>
        <script src="__STATIC__/admin/lib/layui/layui.js"></script>

    </head>
  <body>
    <div class="x-body news-add">
        <form class="layui-form" id="formData">
          <div class="layui-form-item">
              <label for="L_email" class="layui-form-label">
                  <span class="x-red">*</span>标题
              </label>
              <div class="layui-input-inline news-add-input">
              		<input type="hidden" name="id" id="id" value="{$data.id}" />
                  <input type="text"  name="title"
                  autocomplete="off" class="layui-input" value="{$data.title}" placeholder="请输入文章标题">
              </div>
          </div>
          <div class="layui-form-item">
              <label class="layui-form-label"><span class="x-red">*</span>所属分类</label>
              <div class="layui-input-inline news-add-input">
                <select name="news_class">
                {volist name="newsType" id="vo"}
                    {if condition="$data.news_class eq $vo.id"} 
                    		<option value="{$vo.id}" selected = "selected">{$vo.name}</option>
						{else /}
							<option value="{$vo.id}">{$vo.name}</option>
					{/if}
                {/volist}
                </select>
              </div>
          </div>
          <div class="layui-form-item">
              <label for="L_username" class="layui-form-label">
                  <span class="x-red">*</span>关键词
              </label>
              <div class="layui-input-inline news-add-input">
                  <input type="text"  name="keywords"
                  autocomplete="off" class="layui-input" value="{$data['keywords']}" placeholder="请输入文章关键词，多个关键词使用英文逗号隔开">
              </div>
          </div>
          <div class="layui-form-item">
              <label for="L_pass" class="layui-form-label">
                  <span class="x-red">*</span>描述
              </label>

              <div class="layui-input-inline news-add-input">
                  <textarea placeholder="请输入文章摘要" class="layui-textarea" name="desc">{$data['desc']}</textarea>
              </div>
          </div>
          <div class="layui-form-item">
              <label for="L_repass" class="layui-form-label">
                  <span class="x-red">*</span>发表时间
              </label>
              <div class="layui-input-inline news-add-input">
                  <input type="text" id="createTime" name="create_time" 
                  autocomplete="off" class="layui-input" value="{$data['create_time']}" placeholder="请选择发表时间">
              </div>
              
          </div>

          <div class="layui-form-item">
              <label for="L_repass" class="layui-form-label">
                  <span class="x-red">*</span>浏览数
              </label>
              <div class="layui-input-inline news-add-input">
                  <input type="text" name="view_times"
                  autocomplete="off" class="layui-input" value="{$data['view_times']}">
              </div>
          </div>

          <div class="layui-form-item">
              <label for="L_repass" class="layui-form-label">
                  <span class="x-red">*</span>缩略图
              </label>
              <div class="layui-input-inline news-add-input">
                  <div class="fileInput left" style="background: url('__STATIC__/admin/images/upFileBtn.png') left center no-repeat;">
                    <input type="file" name="zoom_img" id="upfile" class="upfile" onchange="handleChangePic();" hidden="hidden" />
                    <input class="upFileBtn" type="button" value="上传图片" onclick="document.getElementById('upfile').click()" />
                    <span class="tip left" id="upfileResult" style="margin-left: 15px;">图片大小不超过2M,大小90*90,支持jpg、png、bmp等格式。</span>
                  </div>
              </div>
          </div>
              
          <div class="layui-form-item">
              <label for="L_repass" class="layui-form-label">
                  <span class="x-red">*</span>正文
              </label>
              <div class="layui-input-inline news-add-input" style="height: 500px;">
                  <div id="summernote"></div>
              </div>
          </div>
          <div class="layui-form-item">
              <label for="L_repass" class="layui-form-label">
              </label>
              <button  class="layui-btn" lay-filter="add" id="createArticle">
                  	确定修改
              </button>
          </div>
      </form>
    </div>
         <script src="__STATIC__/admin/lib/layui/layui.js" charset="utf-8"></script>
<script type="text/javascript" src="__STATIC__/admin/js/xadmin.js"></script>
     <script>
            function handleChangePic () {
                // document.getElementById('upfileResult').innerHTML =this.value;
                $('#upfileResult').html($('#upfile').val());
            }

            //调用富文本编辑
            $(document).ready(function() {
                layui.use('laydate', function(){
                  var laydate = layui.laydate;
                  
                  //执行一个laydate实例
                  laydate.render({
                    elem: '#createTime' //指定元素
                  });
                });
                var $summernote = $('#summernote').summernote({
                    height: 450,
                    lang: 'zh-CN',
                    minHeight: null,
                    maxHeight: null,
                    focus: true,
                    placeholder: '请输入文章正文内容，支持插入图片和代码编辑模式',
                    
                    //调用图片上传
                    callbacks: {
                        onInit: function () {
                            $('#summernote').summernote('code', '{$data.text}');
                        },
                        onImageUpload: function(files) {
                            sendFile($summernote, files[0]);
                        }
                    }
                });
 
                //ajax上传图片
 
                function sendFile($summernote, file) {
                    var formData = new FormData();
                    formData.append("file", file);
                    $.ajax({
                        url: "{:url('admin/news/upload')}", //路径是你控制器中上传图片的方法，下面controller里面我会写到
                        data: formData,
                        cache: false,
                        contentType: false,
                        processData: false,
                        type: 'POST',
                        success: function(data) {
                            // var response = JSON.parse(data);
                            // console.log(data);
                            $summernote.summernote('insertImage', data, function($image) {
                                $image.attr('src', data);
                            });
                        }
                    });
                }
                $('#createArticle').on('click', function() {
                	var obj = {};
                  	var param = $('#formData').serialize();//得到一整串的字符串
                    var paramList = param.split('&');//得到一个数组
                    paramList.map(function (item, index) {
                    	var temp = item.split('=');
                    	obj[temp[0]] = temp[1];
                    });
//                  obj['zoom_img'] = $('#upfile').val();
                    obj['text'] = $('#summernote').summernote('code');
                    
                    for (var i in obj) {
					    if (!obj[i]) {
					    	layer.msg('所有必填项不得为空', { 'icon': 2,'time': 1000});
					    	return false;
					    }
					}
					if (obj['text'] == '<p><br></p>') {
                    	layer.msg('正文不得为空', { 'icon': 2,'time': 1000});
					    return false;
                   }
                    var formData = new FormData($('#formData')[0]);
                    formData.append("text", obj['text']);
                    if ($('#upfile').val()) {
                    	formData.append("zoom_img", $('#upfile').val());
                    }
                    
                    
                    $.ajax({
                        type: 'post',
                        url: "{:url('admin/news/update_news')}",
                        type: 'POST',  
                        data: formData,  
                        cache: false,  
                        processData: false,  
                        contentType: false , 
                        success: function(res) {
                        	if (res.success) {
                        		layer.msg('修改成功', { 'icon': 1,'time': 1000},function () {
                        			var index = parent.layer.getFrameIndex(window.name);
                            		parent.layer.close(index);
                            		window.localStorage.setItem('update_news_list',new Date().getTime());
                        		});
                        	} else {
                        		layer.msg('修改失败，无任何更新', { 'icon': 2,'time': 1000});
                        	}
                        },
                        error: function () {
                        	layer.msg('修改失败', { 'icon': 2,'time': 1000});
                        }
                    });
                    return false;
                });
            });
        </script>
  </body>

</html>